import { formDataFormat } from '../../utils';

import { getWaitApprovallist, fetAgree, fetrefuse, sendBack, queryDetail } from '../services';
import { queryProcess } from '../../services';

export default {
    namespace: 'flowWaitApproval',
    state: {
        waitApprovalList: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        order: '',
        action: '',
        // 高级搜索字段
        title: '',
        startTime: '',
        endTime: '',
        templateNameList: [],
        status: '',
        detailData: {},
        processList: [],
        canReturnList: []
    },
    effects: {
        // 列表
        *fetchWaitApprovalList({ payload }, { call, put }) {
            const response = yield call(getWaitApprovallist, payload);
            if (response && response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        // order: payload.orderBy,
                        waitApprovalList: response.data.rows,
                        pagination: {
                            pageSize: response.data.pageSize,
                            current: response.data.pageNo,
                            total: response.data.total
                        }
                    }
                });
            }
        },
        // 同意
        *fetchAgree({ payload, callback }, { call }) {
            const response = yield call(fetAgree, payload);
            if (callback) {
                callback(response);
            }
        },
        // 拒绝
        *fetchRefuse({ payload, callback }, { call }) {
            const response = yield call(fetrefuse, payload);
            if (callback) {
                callback(response);
            }
        },
        // 退回
        *fetchReturn({ payload, callback }, { call }) {
            const response = yield call(sendBack, payload);
            if (callback) {
                callback(response);
            }
        },
        // 获取详情
        *fetchDetail({ payload, callback }, { call, put }) {
            const response = yield call(queryDetail, payload);
            if (response && response.code === 10000) {
                const returnData = response.data;
                // 把返回的表单值和表单做处理
                const newData = formDataFormat(returnData);

                yield put({
                    type: 'save',
                    payload: {
                        detailData: { ...returnData, formList: newData }
                    }
                });
                if (callback) {
                    callback(response);
                }
            }
        },
        // 获取审批节点
        *fetchProcess({ payload, callback }, { call, put }) {
            const response = yield call(queryProcess, payload);
            yield put({
                type: 'save',
                payload: {
                    processList: [],
                    canReturnList: []
                }
            });
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        processList: response.data
                    }
                });
                // 过滤得出有效节点, 因为服务端是把所有的审批历史返回给前端, 前端要通过历史记录来过滤出可回退的节点
                // 场景说明:
                // 假设流程是这样的 [A -> B -> C -> B -> C -> A]
                // 可回退的节点变化 []  [A]  [A, B]  [A]  [A, B]  []
                const validData = response.data.filter(item => item.type === '审批人' && item.status);
                let canReturnList = [];
                validData.forEach((item, index) => {
                    if (item.status === 6) {
                        // 如果是回退节点
                        // 回退的节点是节点列表的下一个节点
                        const nextNode = validData[index + 1];
                        // 查找要退回的节点, 并截断该节点之后的数据
                        const returnIndex = canReturnList.findIndex(node => node.taskDefKey === nextNode.taskDefKey);
                        canReturnList = canReturnList.slice(0, returnIndex);
                    } else if (item.status !== 2) {
                        // 2 是审批中的节点要排除
                        canReturnList.push(item);
                    }
                });
                // 保存
                yield put({
                    type: 'save',
                    payload: {
                        canReturnList
                    }
                });
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        processList: []
                    }
                });
            }
            if (callback) {
                callback(response);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        resetState(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
